<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@include file="common/head.jsp"%>
<script>
    window.json=null;
</script>
<div class="layui-body" onload="init();">
    <div style="padding: 15px">
        <div class="layui-tab-item layui-show">
            <table lay-filter="table_approval" id="table_approval"></table>
        </div>
    </div>
</div>
<%@include file="/jsp/root/common/foot.jsp" %>
<script type="text/javascript">
    /*初始化table*/
    function init() {
        sessionStorage.setItem("flag","no");
        render();
    }
    window.onload=init;
    /*查询数据*/
    function render() {
        layui.use(['form','table','jquery'],function () {
            let table = layui.table;
            table.render({
                elem: '#table_approval'
                , size: 'lg'
                , even: true
                , skin: 'row'
                ,page:true
                ,limit:9
                ,limits:[1,5,9]
                ,autoSort: false
                , cols: [[
                    {
                        field: 'applyID'
                        , unresize: 'true'
                        , width: '7%'
                        , title: '申请ID'
                    }
                    , {
                        field: 'userCode'
                        , unresize: 'true'
                        , width: '11%'
                        , title: '用户ID'
                    }
                    , {
                        field: 'userName'
                        , unresize: 'true'
                        , width: '8%'
                        , title: '用户姓名'
                    }
                    , {
                        field: 'userRole'
                        , unresize: 'true'
                        , width: '8%'
                        , title: '用户类型'
                    }
                    , {
                        field: 'labName'
                        , unresize: 'true'
                        , width: '10%'
                        , title: '申请实验室'
                    }
                    , {
                        field: 'interval'
                        , unresize: 'true'
                        , width: '9%'
                        , title: '申请时段'
                    }
                    , {
                        field: 'apply_reason'
                        , unresize: 'true'
                        , width: '9%'
                        , title: '申请理由'
                    }
                    , {
                        field: 'apply_creationDate'
                        , unresize: 'true'
                        , width: '10%'
                        , title: '申请时间'
                    }
                    , {
                        field: 'operation'
                        , unresize: 'true'
                        , title: '申请操作'
                        ,toolbar:apply
                    }
                ]]
                ,url:'${pageContext.request.contextPath}/jsp/root.do?method=getApprovalMsg'
                ,parseData:function (res) {
                    let result;
                    if(this.page.curr){
                        result = res.data.slice(this.limit*(this.page.curr-1),this.limit*this.page.curr);
                    }
                    else{
                        result=res.data.slice(0,this.limit);
                    }
                    return{
                        "code":0
                        ,"data":result
                        ,"count":res.total
                    }
                }
            });
        });
    }
</script>
<script type="text/javascript" id="apply">

    <a class="layui-btn layui-btn-radius layui-btn-normal" lay-event="detail">
        <i class="layui-icon">&#xe670;</i> 详情
    </a>

    <a class="layui-btn layui-btn-radius layui-btn-warm" lay-event="agree">
        <i class="layui-icon">&#x1005;</i> 同意
    </a>
    <a class="layui-btn layui-btn-radius layui-btn-danger" lay-event="refuse">
        <i class="layui-icon">&#x1006;</i> 拒绝
    </a>

</script>
<script type="text/javascript">
    layui.use('table',function () {
        let table = layui.table;
        table.on('tool(table_approval)',function (obj) {
            let data = obj.data;
            json=JSON.stringify(data);
            jsonData = eval('('+JSON.stringify(data)+')');
            if (obj.event === 'detail'){
                detail();
            }else if (obj.event === 'agree'){
                agree(jsonData);
            }else if (obj.event === 'refuse'){
                refuse(jsonData);
            }
        })
    })
</script>
<script>
    function detail() {
        layer.open({
            type: 2,//1：直接写面板 2：链接面板
            title: '实验室详情',
            shadeClose: true,
            skin: 'yourclass',
            content: "<%=path%>/jsp/root/eject/details.jsp",//链接面板
            area: ['30%', '50%'],//面板大小
            anim:5,
        })
    }
    function agree(jsonData) {
        $.ajax({
            type: "POST"
            ,dataType:"json"
            ,url: "${pageContext.request.contextPath}/jsp/root.do?method=agree&applyID="+jsonData.applyID
            ,success:function (data) {
                if (data.ok === "yes"){
                    layui.use('layer',function(){
                        var layer=layui.layer;
                        layer.msg('已同意',{time:2000});
                        render();
                    });
                }else if (data.ok === "no"){
                    layui.use('layer',function(){
                        var layer=layui.layer;
                        layer.msg('审批失败',{time:2000});
                    });
                }
            }
            ,error:function () {
                layui.use('layer',function(){
                    var layer=layui.layer;
                    layer.msg('出了一个问题，导致审批失败了',{time:2000});
                });
            }
        });
    }
    function refuse(jsonData) {
        $.ajax({
            type: "POST"
            ,dataType:"json"
            ,url: "${pageContext.request.contextPath}/jsp/root.do?method=refuse&applyID="+jsonData.applyID
            ,success:function (data) {
                if (data.ok === "yes"){
                    layui.use('layer',function(){
                        var layer=layui.layer;
                        layer.msg('已拒绝',{time:2000});
                        render();
                    });
                }else if (data.ok === "no"){
                    layui.use('layer',function(){
                        var layer=layui.layer;
                        layer.msg('审批失败',{time:2000});
                    });
                }
            }
            ,error:function () {
                layui.use('layer',function(){
                    var layer=layui.layer;
                    layer.msg('出了一个问题，导致审批失败了',{time:2000});
                });
            }
        });
    }
</script>
</body>

</html>